﻿<%@ Page %>
<HTML>
	<HEAD>
		<title>增加字段</title><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">
		<link rel="stylesheet" type="text/css" href="/_common/styles/global.css">
			<link rel="stylesheet" type="text/css" href="/_common/styles/select.css">
				<link rel="stylesheet" type="text/css" href="/_nav/tabs.css">
					<link rel="stylesheet" type="text/css" href="/Tools/FormEditor/Styles/dialogs.css">
						<style> TD.gridspacer { width: 2px; }
	TD.gridheader { padding-left: 8px; }
	TABLE.gridheader { background-color: #e9e7d7; background-image: url(/_grid/imgs/bar_bk.gif); font-size: 11px; width: 100%; height: 22px; border: 1px solid #dbdac9; border-bottom: 1px solid #c5c1b1; }
	</style>
						<script language="javascript" src="/_controls/settings.aspx"></script>
						<script language="javascript" src="/_common/scripts/global.js"></script>
						<script language="javascript" src="/Tools/FormEditor/Scripts/objects.js"></script>
						<script language="javascript" src="/Tools/FormEditor/Scripts/dialogs.js"></script>
						<script language="javascript" src="/Tools/FormEditor/Scripts/fielddialogs.js"></script>
						<script language="JavaScript">

	var	_oLastGlow;
	var _oFieldsXml;
	var _oPropertiesXml;
	var _oFormXml;
	
	var _oXmlDoc = new ActiveXObject("Msxml2.DOMDocument");
	_oXmlDoc.async = false;

	var _oXslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
	_oXslDoc.async = false;

	var _oXslt = new ActiveXObject("Msxml2.XSLTemplate");
	
	var _oXslProc;


	function window.onload()
	{
		_oFieldsXml		= window.dialogArguments.FieldsXml;
		_oPropertiesXml	= window.dialogArguments.FieldPropertiesXml;
		_oFormXml		= window.dialogArguments.FormXml;
	
		// build the list of available fields
		//
		_oXslDoc.load("/Tools/FormEditor/Dialogs/fields.xsl");
		
		_oXslt.stylesheet = _oXslDoc;
		
		_oXmlDoc.loadXML(GetFieldsXml());
		
		_oXslProc = _oXslt.createProcessor();
		_oXslProc.input = _oXmlDoc;

		_oXslProc.transform();
		
		FieldList.innerHTML = _oXslProc.output;
		
		BuildLocations(window.dialogArguments.TabName, window.dialogArguments.SectionName)
	}


	function Ok()
	{
		var oFields = new Array();
		var oDisplayNames;

		var i = 0;
		var ii = 0;
		var iLen = Fields.rows.length;

		while (i < iLen)
		{
			if (Fields.rows[i].cells[0].firstChild.checked)
			{
				oDisplayNames = new Array(new LocalizedObj(Fields.rows[i].cells[1].innerText, ORG_LANGUAGE_CODE));
				
				oFields[ii]	= new FieldObj(
								crmDialog.TabName.returnValue,
								crmDialog.SectionName.returnValue,
								Fields.rows[i].cells[2].innerText,
								Fields.rows[i].cells[1].innerText,
								"",
								Fields.rows[i].cells[3].innerText,
								(Fields.rows[i].required == "true"),
								Fields.rows[i].orgrequired,
								oDisplayNames,
								oDisplayNames,
								null,
								null,
								null
							);

				ii++;
			}

			i++;
		}
		
		window.returnValue = (!oFields.length) ? false : oFields;

		window.close();
	}


	// returns a sting of xml containing all of the available fields
	//
	function GetFieldsXml()
	{			
		var sFieldName;
		var sFieldLocName;

		var sFilter = BuildUsedFieldFilter(_oFormXml.selectNodes("/entity/form/tabs/tab/sections/section/rows/row/cell[@name]")) + " and @name != 'statecode'";

		// get all the field nodes from the field properties xml minus the fields already in use by the form
		// 从field properties xml中获取字段节点，排除在form中使用的字段
		//
		var oFields = _oPropertiesXml.selectNodes("/entity/fields/field" + ((sFilter.length > 0) ? "[" + sFilter + "]" : ""));
		
		// add each unused field to the fields xml which will later be passed to the add fields dialog
		//
		var sFieldsXml = "<fields>";
		
		var i;
		var iLen = oFields.length;
		
		for (i = 0; i < iLen; i++)
		{
			sFieldName = oFields[i].getAttribute("name");
			
			sFieldsXml += "<field name='" + sFieldName + "' displayname=\"" + oFields[i].selectSingleNode("displaynames/displayname[@languagecode = '" + ORG_LANGUAGE_CODE + "']/@description").text + "\" datatype='" + _oFieldsXml.selectSingleNode("/entity/fields/field[@name = '" + sFieldName + "']/@datatype").text + "' required='" + _oFieldsXml.selectSingleNode("/entity/fields/field[@name = '" + sFieldName + "']/@requiredforplatform").text + "' orgrequired='" + _oPropertiesXml.selectSingleNode("/entity/fields/field[@name = '" + sFieldName + "']/@requiredlevel").text + "'/>";
		}
		
		// get all the field nodes from the entity xml minus the fields already returned from the field properties xml
		// 从entity xml中获取所有字段节点，排除field properties xml中的字段
		//
		var sTemp = BuildUsedFieldFilter(oFields);

		if (sTemp.length > 0)
		{
			if (sFilter.length > 0)
			{
				sFilter += " and " + sTemp;
			}
			else
			{
				sFilter = sTemp;
			}
		}
		
		oFields = _oFieldsXml.selectNodes("/entity/fields/field" + ((sFilter.length > 0) ? "[" + sFilter + " and @validforform = 'true' and @datatype != 'uniqueidentifier' and @datatype != 'timezone']" : ""));
		
		iLen = oFields.length;
		
		for (i = 0; i < iLen; i++)
		{
			sFieldName		= oFields[i].getAttribute("name");
			sFieldLocName	= oFields[i].getAttribute("localizedname");
			
			sFieldsXml += "<field name='" + sFieldName + "' displayname='" + ((sFieldLocName) ? sFieldLocName : sFieldName) + "' datatype='" + oFields[i].getAttribute("datatype") + "' required='" + _oFieldsXml.selectSingleNode("/entity/fields/field[@name = '" + sFieldName + "']/@requiredforplatform").text + "' orgrequired='na'/>";
		}
		
		sFieldsXml += "</fields>";
		
		return sFieldsXml;
	}
	
	
	function BuildUsedFieldFilter(oNodes)
	{
		var sFilter = "";

		var i;
		var iLen = oNodes.length;

		for (i = 0; i < iLen; i++)
		{
			if (i != 0)
			{
				sFilter += " and ";
			}
			
			sFilter += "@name != '" + oNodes[i].getAttribute("name") + "'";
		}

		return sFilter;
	}
	
	
	function SelectAll()
	{
		var o = event.srcElement;
		
		var bIsChecked = o.checked;
		
		var i;
		var iLen = Fields.rows.length;
		
		for (i = 0; i < iLen; i++)
		{
			Fields.rows[i].cells[0].children[0].checked = bIsChecked;
		}
	}


	function on()
	{
		_oLastGlow = event.srcElement;

		while (_oLastGlow.tagName != "TR")
		{
			_oLastGlow = _oLastGlow.parentElement;
		}

		_oLastGlow.runtimeStyle.backgroundColor = "#ADC3E7";
	}

	function off()
	{
		if (_oLastGlow) _oLastGlow.runtimeStyle.backgroundColor = "";
	}
	
						</script>
	</HEAD>
	<body>
		<form name="crmDialog">
			<table border="0" cellspacing="0" cellpadding="0" width="100%" height="100%">
				<tr>
					<td class="header">
						<div class="header"><!-Add Fields->增加字段</div>
						<div class="headerdesc"><!-Select the fields to add to the form.->选择要增加到表单的字段</div>
					</td>
				</tr>
				<tr>
					<td class="main">
						<table width="100%" cellspacing="0" cellpadding="0">
							<tr height="22">
								<td>
									<table cellpadding="0" cellspacing="0" class="gridheader">
										<tr>
											<td width="24"><input type="checkbox" class="checkbox" onclick="SelectAll();"></td>
											<td class="gridspacer"><img src="/_grid/imgs/bar_line.gif"></td>
											<td class="gridheader"><!-Field Name->字段名</td>
											<td class="gridspacer"><img src="/_grid/imgs/bar_line.gif"></td>
											<td class="gridheader" width="168"><!-Schema Name->显示名</td>
											<td class="gridspacer"><img src="/_grid/imgs/bar_line.gif"></td>
											<td class="gridheader" width="64"><!-Type->类型</td>
											<td class="gridspacer"><img src="/_grid/imgs/bar_line.gif"></td>
											<td width="14">&nbsp;</td>
										</tr>
									</table>
								</td>
							</tr>
							<tr>
								<td>
									<div id="FieldList" style="OVERFLOW-Y: scroll; OVERFLOW-X: hidden; BORDER-LEFT: #cccccc 1px solid; HEIGHT: 150px; BACKGROUND-COLOR: #ffffff">
									</div>
								</td>
							</tr>
							<tr height="22">
								<td>
									<table cellpadding="0" cellspacing="0" class="gridheader">
										<tr>
											<td>&nbsp;</td>
										</tr>
									</table>
								</td>
							</tr>
						</table>
						<br>
						<fieldset>
							<legend><!-Location->提示&nbsp;</legend>
							<div class="desc"><!-Select the tab and section for this field to appear under. If 
								a tab does not contain sections, it will not appear in the list.-> 
								选择本字段要出现的标签和区域；如果一个标签没有区域，它将不能出现在列表中。
							</div>
							<div id="FieldLocations">
							</div>
						</fieldset>
					</td>
				</tr>
				<tr>
					<td class="buttons">
						<button id="btnDialogOk" onclick="Ok();" type="button">确定</button>
						&nbsp;
						<button id="btnDialogCancel" onclick="Cancel();" type="button">取消</button>
					</td>
				</tr>
			</table>
		</form>
	</body>
</HTML>
